#include<bits/stdc++.h>
#define int long long
#define re register
#define rep(i,a,b) for(int i(a);i<=(b);++i)
#define req(i,a,b) for(int i(a);i>=(b);--i)
using namespace std;
#ifdef ONLINE_JUDGE
char buf[(1<<21)+5],*p1,*p2;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#endif
template<typename T> inline int read(T &num){
	int f=0,x=0;
	char ch=getchar();
	while(!isdigit(ch)){f|=(ch=='-');ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return num=f?-x:x;
}
inline int Read()
{
	re int x=0,f=0;re char ch=getchar();
	while(!isdigit(ch)) f|=ch=='-',ch=getchar();
	while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return f?-x:x;
}
template<typename T> inline void write(T num)
{
	num=num<0?putchar('-'),-num:num;
	num>9?write(num/10),0:0;
	putchar(num%10+48);
}
template<typename T> inline void writeln(T num)
{
	write(num);
	putchar('\n');
}
///////////////////////////////////////////////////////////////
#define ________ y1
#define ___________ y2
int qzh[1003][1003],a[1003][1003],n,m,q,x1,y1,x2,y2,p,ans;
int qpow(int x,int y)
{
	if(y==0) return 1;
	if(y==1) return x%p;
	int mid=qpow(x,y>>1)%p;
	return (y&1)?mid*mid%p*x%p:mid*mid%p;
}
int inv(int x)
{
	return qpow(x,p-2);
}
bool isprime(int x)
{
	if(x==2||x==3)return 1;
	for(int i=2;i<=sqrtl(x);++i) if(x%i==0) return 0;
	return 1;
}
void solve_prime()
{
	rep(i,0,n+1)rep(j,0,m+1)qzh[i][j]=1;
	rep(i,1,n)rep(j,1,m)(qzh[i][j]=a[i][j]*qzh[i-1][j]%p*qzh[i][j-1]%p*inv(qzh[i-1][j-1]))%=p;
	read(q);
	rep(i,1,q)
	{
		read(x1);read(y1);read(x2);read(y2);
		(ans+=(qzh[x2][y2]*inv(qzh[x1-1][y2])%p*inv(qzh[x2][y1-1])%p*qzh[x1-1][y1-1]%p)^i)%=147744151;
	}
	write(ans);
}
void baoli()
{
	read(q);
	rep(i,1,q)
	{
		read(x1);read(y1);read(x2);read(y2);
		int res=1;
		rep(X,x1,x2)rep(Y,y1,y2) (res*=a[X][Y])%=p;
//		writeln(res);
		ans+=res^i;ans%=147744151;
	}
	write(ans);
}
signed main()
{
	freopen("tsuki.in","r",stdin);
	freopen("tsuki.out","w",stdout);
	read(n);read(m);read(p);
	rep(i,1,n)rep(j,1,m) read(a[i][j]);
	if(n<=500&&m<=500) baoli();
	else solve_prime();
    return 0;
}
// 36pts
